package com.cskaoyan.dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/line")
public class Demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        // http://localhost:8080/demo3/line?username=zhangsan
        // http://192.168.106.25:8080/demo3/line?username=zhangsan
        // 192.168.106.25可以修改为自己的ip

        printLine(request, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        printLine(req,resp);
    }

    private static void printLine(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        // 请求方法
        String method = request.getMethod();
        // 请求URL和URI
        String url = request.getRequestURL().toString();
        String uri = request.getRequestURI();

        // 服务器本地的IP和端口号
        String localAddr = request.getLocalAddr();
        int localPort = request.getLocalPort();

        // 应用的上下文路径
        String contextPath = request.getContextPath();

        String queryString = request.getQueryString();//username=zhangsan

        // 请求协议
        String protocol = request.getProtocol();

        resp.getWriter().println(method);
        resp.getWriter().println(url);
        resp.getWriter().println(uri);
        resp.getWriter().println(localAddr);
        resp.getWriter().println(localPort);
        resp.getWriter().println(contextPath);
        resp.getWriter().println(queryString);
        resp.getWriter().println(protocol);
    }


}
